$(function(){
    //列表异步点击保存
    $('.ajax_action').click(function(){
        var data = $(this).parents('tr').data(),
            data2 = $(this).data(),
            tip = $(this).attr('tip') || "操作",
            url = $(this).attr('url') || "";
        $.extend(data, data2);
        swal({
            title: "您确定要"+tip+"吗？",text: "该操作很重要，请谨慎操作！",confirmButtonText: "确定",type: "warning",showCancelButton: true,confirmButtonColor: "#DD6B55",cancelButtonText: "让我再考虑一下…",closeOnConfirm: false
        }, function (isConfirm) {
            if (isConfirm) {
                $.ajax({
                    type: "post",
                    url: url,
                    data: data,
                    dataType: "json",
                    success: function(param){
                        if(param.code == '20000'){
                            swal(tip+"成功！", param.message, "success");
                            setTimeout(()=>{location.reload()},1500);
                        }else{
                            swal(tip+"失败！", "此次操作失败。", "error")
                        }
                    }
                });
            }
        });
    });

    //弹出框异步点击保存
    $('.ajax_modal_action .save_btn').click(function(){
        var modal = $(this).parents('.ajax_modal_action'),
            data = modal.find('form').serialize(),
            url = modal.find('form').attr('data-url');
        $.ajax({
            type: "post",
            url: url,
            data: data,
            dataType: "json",
            success: function(param){
                if(param.code == '20000'){
                    swal("操作成功！", param.message, "success");
                    setTimeout(()=>{location.reload()},1500);
                }else{
                    swal("操作失败！", "此次操作失败。", "error")
                }
            }
        });
    });

    //改变地区选择框
    $('body').on('change','.select_area select',function(){
        var type = $(this).attr('class'),
            id = $(this).val();
        $(this).nextAll('select').find('option:gt(0)').remove();
        if(type == 'country' || id === '') return ;
        get_area(id,$(this).next());
    });
    //新增一行区域
    $('.add_area_box .add_area').click(function(){
        prov_select_i++;
        $('.add_area_box').before('<div class="form-group">\
                <label class="col-sm-2 control-label"></label>\
                <div class="col-sm-5 select_area">\
                <select name="area['+prov_select_i+'][province]" class="province"><option value="">选择省份</option></select>\
                <select name="area['+prov_select_i+'][city]" class="city"><option value="">选择城市</option></select>\
                <select name="area['+prov_select_i+'][country]" class="country"><option value="">选择地区</option></select>\
                <button type="button" class="btn btn-info btn-xs del_area">删除</button>\
                </div>\
             </div>');
        get_area(0,$('.select_area:last .province'));
    });
    //删除一行区域
    $('body').on('click','.select_area .del_area',function(){
        $(this).parents('.form-group').remove();
    });
});


//根据id获取下级区域，并填充select
var prov_select_html = '';
var prov_select_i = 10000;
function get_area(id,obj){
    if(id == 0 && prov_select_html != ''){
        obj.append(prov_select_html);
        return true;
    }
    $.post('/site/getarea',{'id':id},function(res){
        if(res.code == 20000 && res.data != undefined){
            var html = '';
            $.each(res.data,function(i,n){
                html+='<option value="'+ n.id+'">'+ n.name+'</option>';
            });
            if(id == 0){
                prov_select_html = html;
            }
            obj.append(html);
        }
    },'json');
}

//oss 图片转换
function image_resize(data){
    if(data.url == undefined){
        return '';
    }
    if(data.width != undefined && data.high != undefined){
        return data.url + '?x-oss-process=image/resize,m_fixed,h_'+data.high+',w_'+data.width;
    }
    if(data.width != undefined){
        return data.url + '?x-oss-process=image/resize,w_'+data.width;
    }
    if(data.high != undefined){
        return data.url + '?x-oss-process=image/resize,h_'+data.high;
    }
    return data.url;
}